<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="景区名称" prop="scenicSpotName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.scenicSpotName"-->
      <!--          placeholder="请输入景区名称"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="开放时间" prop="openHours">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.openHours"-->
      <!--          placeholder="请输入开放时间"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="景区地址" prop="scenicSpotAddress">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.scenicSpotAddress"-->
      <!--          placeholder="请输入景区地址"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="景区关闭时间" prop="clearHours">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.clearHours"-->
      <!--          placeholder="请输入景区关闭时间"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <!--        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:spot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="景区图片" align="center" prop="scenicSpotPicture" >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img :src="scope.row.scenicSpotPicture" alt="" style="width: 200px;height: 200px">
            <img slot="reference" :src="scope.row.scenicSpotPicture" style="width: 30px;height: 30px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="景区名称" align="center" prop="scenicSpotName" />
      <el-table-column label="开放时间" align="center" prop="openHours" />
      <el-table-column label="关闭时间" align="center" prop="clearHours" />
      <el-table-column label="联系电话" align="center" prop="scenicSpotPhone" />
      <el-table-column label="景区地址" align="center" prop="scenicSpotAddress" />
      <!--      <el-table-column label="票务信息" align="center" prop="ticketingInformation" />-->
      <!--      <el-table-column label="景区介绍" align="center" prop="scenicSpotIntroduce" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="addfood(scope.row)"
            v-hasPermi="['system:spot:edit']"
          style="color: red">添加景点</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:spot:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:spot:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改景区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="景区图片">
          <uploadImage v-model="form.scenicSpotPicture"/>
        </el-form-item>
        <el-form-item label="景区名称" prop="scenicSpotName">
          <el-input v-model="form.scenicSpotName" placeholder="请输入景区名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="scenicSpotPhone">
          <el-input v-model="form.scenicSpotPhone" placeholder="请输入景区联系电话" />
        </el-form-item>
        <el-form-item label="景区地址" prop="scenicSpotAddress">
          <el-input v-model="form.scenicSpotAddress" placeholder="请输入景区地址" />
        </el-form-item>
        <!--        <el-form-item label="票务信息" prop="ticketingInformation">-->
        <!--          <el-input v-model="form.ticketingInformation" placeholder="请输入票务信息" />-->
        <!--        </el-form-item>-->
        <!-- 添加或修改预约信息配置表对话框 -->
        <el-form-item label="开始时间" prop="openHours">
          <!--开始-->
          <el-time-select
            placeholder="起始时间"
            v-model="form.openHours"
            :picker-options="{
              start: '00:00',
               step: '00:30',
               end: '24:00'
      }">
          </el-time-select>
        </el-form-item>
        <el-form-item label="结束时间" prop="clearHours">
          <!--  结束-->
          <el-time-select
            placeholder="结束时间"
            v-model="form.clearHours"
            :picker-options="{
      start: '00:00',
      step: '00:30',
      end: '24:00',
      minTime: openHours
    }"></el-time-select></el-form-item>
        <el-form-item label="语音介绍" prop="scenicSpotIntroduce">
          <UploadFile v-model="form.scenicSpotIntroduce" />
        </el-form-item>
        <el-form-item label="景区介绍">
          <editor v-model="form.scenicSpotIntroducetext" :min-height="192"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listSpot, getSpot, delSpot, addSpot, updateSpot, exportSpot } from "@/api/system/spot";
  import UploadImage from '@/components/UploadImage';
  import Editor from '@/components/Editor';
  import UploadFile from '@/components/FileUpload';
  export default {
    name: "Spot",
    components: {
      UploadImage,
      Editor,
      UploadFile,
    },
    data() {
      return {
        openHours:'',
        clearHours:'',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 景区表格数据
        spotList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          scenicSpotName: null,
          openHours: null,
          scenicSpotAddress: null,
          clearHours: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          scenicSpotPicture: [
            { required: true, message: "景区图片不能为空", trigger: "blur" }
          ],
          scenicSpotName: [
            { required: true, message: "景区名称不能为空", trigger: "blur" }
          ],
          openHours: [
            { required: true, message: "开放时间不能为空", trigger: "blur" }
          ],
          scenicSpotPhone: [
            { required: true, message: "景区联系电话不能为空", trigger: "blur" }
          ],
          scenicSpotAddress: [
            { required: true, message: "景区地址不能为空", trigger: "blur" }
          ],
          clearHours: [
            { required: true, message: "景区关闭时间不能为空", trigger: "blur" }
          ],
          scenicSpotIntroduce: [
            { required: true, message: "景区介绍不能为空", trigger: "blur" }
          ],
          createTime: [
            { required: true, message: "创建时间不能为空", trigger: "blur" }
          ],
          updateTime: [
            { required: true, message: "更新时间不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      addfood(e){
        console.log(e)
        this.$router.push('/jingqu/jingdian?id=' + e.id+'&scenicSpotName='+e.scenicSpotName)
      },
      /** 查询景区列表 */
      getList() {
        this.loading = true;
        listSpot(this.queryParams).then(response => {
          this.spotList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          scenicSpotPicture: null,
          scenicSpotName: null,
          openHours: null,
          scenicSpotPhone: null,
          scenicSpotAddress: null,
          ticketingInformation: null,
          clearHours: null,
          scenicSpotIntroducetext: null,
          scenicSpotIntroduce: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加景区";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSpot(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改景区";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSpot(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSpot(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除景区编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpot(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有景区数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSpot(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
